import {LOGIN, ROUTES, USERPAGELIST, SAVEUSER, ROLELIST, SAVEROLE, FUNLIST, GETFUN, SAVEFUN, DELFUN, SAVEROLEFUNS, GETROLEFUNS, SAVEMYINFO, SAVEMYPWD} from '@/services/api'
import {request, METHOD, removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password) {
  return request(LOGIN, METHOD.POST, {
    email: name,
    password: password
  })
}

/**
 * 获取动态路由
 * @returns {Promise<*>}
 */
export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}

/**
 * 获取用户列表
 * @param pageIndex
 * @param pageSize
 * @param params
 * @returns {Promise<*>}
 */
export async function getUserPageList(pageIndex, pageSize, params) {
  params.pageIndex = pageIndex
  params.pageSize = pageSize
  return request(USERPAGELIST, METHOD.POST, params)
}

/**
 * 保存用户信息
 * @param params
 * @returns {Promise<*>}
 */
export async function saveUser(params) {
  return request(SAVEUSER, METHOD.POST, params)
}

/**
 * 获取角色列表信息
 * @returns {Promise<*>}
 */
export async function getRoleList() {
  return request(ROLELIST, METHOD.POST)
}

/**
 * 获取角色权限信息
 * @returns {Promise<*>}
 */
export async function getRoleFuns(roleId) {
  return request(GETROLEFUNS, METHOD.GET, {roleId: roleId})
}

/**
 * 保存角色权限
 * @param roleId
 * @param roleFuns
 * @returns {Promise<*>}
 */
export async function saveRoleFuns(roleId, roleFuns) {
  return request(SAVEROLEFUNS, METHOD.POST, {roleId: roleId, funIds: roleFuns})
}

/**
 * 保存角色信息
 * @returns {Promise<*>}
 */
export async function saveRole(params) {
  return request(SAVEROLE, METHOD.POST, params)
}

/**
 * 获取权限列表信息
 * @returns {Promise<*>}
 */
export async function getFunList() {
  return request(FUNLIST, METHOD.POST)
}

/**
 * 获取权限信息
 * @returns {Promise<*>}
 */
export async function getFun(funId) {
  return request(GETFUN, METHOD.GET, {funId: funId})
}

/**
 * 保存权限信息
 * @param params
 * @returns {Promise<*>}
 */
export async function saveFun(params) {
  return request(SAVEFUN, METHOD.POST, params)
}

/**
 * 保存权限信息
 * @param params
 * @returns {Promise<*>}
 */
export async function delFun(funId) {
  return request(DELFUN, METHOD.GET, {funId: funId})
}

/**
 * 保存我的信息
 * @param params
 * @returns {Promise<*>}
 */
export async function saveMyInfo(params) {
  return request(SAVEMYINFO, METHOD.POST, params)
}

/**
 * 保存我的密码
 * @param params
 * @returns {Promise<*>}
 */
export async function saveMyPwd(params) {
  return request(SAVEMYPWD, METHOD.POST, params)
}

export default {
  login,
  logout,
  getRoutesConfig,
  getUserPageList
}
